﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
using System.Web.Mvc;


namespace Jzy.Web
{
    public class Global : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        }


        protected void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            //AppDispatch.Dispatch.Start();
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            HttpContext app = System.Web.HttpContext.Current;

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext app = ((HttpApplication)sender).Context;

            string url = app.Request.RawUrl.ToLower();



            //if (url.Contains("/photos.aspx"))
            //{
            //    app.RewritePath("/website/photos.aspx");
            //    //app.Server.Transfer("/website/photos.aspx");
            //}
            //else if (url.Contains("index.aspx") && !url.Contains("/website/index.aspx"))
            //{
            //    app.RewritePath("/website/index.aspx");
            //    // app.Server.Transfer("/website/index.aspx");
            //}
            //else if (url.Contains("articlelist") && !url.Contains("?"))
            //{
            //    int begin = url.IndexOf('-');
            //    int end = url.IndexOf('.');
            //    string menuCode = url.Substring(begin + 1, end - begin - 1);

            //    app.RewritePath("/website/ArticleList.aspx?MenuCode=" + menuCode);
            //}
            //else if (url.Contains("articledetail") && !url.Contains("?"))
            //{
            //    int begin = url.IndexOf('-');
            //    int end = url.IndexOf('.');

            //    string tmp = url.Substring(begin + 1, end - begin - 1);
            //    if (tmp.Contains("m"))
            //    {
            //        app.RewritePath("/website/ArticleDetail.aspx?MenuCode=" + tmp.Substring(1));
            //    }
            //    else
            //    {
            //        app.RewritePath("/website/ArticleDetail.aspx?ID=" + tmp);
            //    }
            //}
            //else if (url.Contains("picturelist") && !url.Contains("?"))
            //{
            //    int begin = url.IndexOf('-');
            //    int end = url.IndexOf('.');
            //    string menuCode = url.Substring(begin + 1, end - begin - 1);

            //    app.RewritePath("/website/PictureList.aspx?MenuCode=" + menuCode);
            //}

            //else if (app.Request.RawUrl.Contains("1"))
            //{
            //    app.RewritePath("WebForm5.aspx");
            //}
            //app.RewritePath(


        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

            //HttpApplication application = (HttpApplication)source;

            //HttpContext context = application.Context;
            // CSException csException = context.Server.GetLastError() as CSException;



            //if (csException == null)
            //    csException = context.Server.GetLastError().GetBaseException() as CSException;

            //try
            //{
            //    if (csException != null)
            //    {
            //        errorMessage = csException.Message;
            //        csException.Log();
            //    }
            //    else
            //    {
            //        Exception ex = context.Server.GetLastError();
            //        if (ex.InnerException != null)
            //            ex = ex.InnerException;

            //        csException = new CSException(CSExceptionType.UnknownError, ex.Message, context.Server.GetLastError());
            //        errorMessage = csException.Message;

            //        System.Data.SqlClient.SqlException sqlEx = ex as System.Data.SqlClient.SqlException;
            //        if (sqlEx == null) csException.Log();//don't log time outs  Old:if (sqlEx == null || sqlEx.Number != -2) 

            //    }
            //}
            //catch { } //not much to do here, but we want to prevent infinite looping with our error handles            


            //errorMessage = Globals.UrlEncode(errorMessage);

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }


        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
           // HttpContext app = System.Web.HttpContext.Current;

            string rawUrl = HttpContext.Current.Request.RawUrl;


            //if (rawUrl.Contains("index.aspx") || (rawUrl.ToLower().Contains("/website") && rawUrl.Contains(".aspx")))
            //{
            //    new IPBO().Save();
            //    new PVBO().Save();
            //}
            //if (((rawUrl == "/") || rawUrl.ToLower().Contains("/site/")) && !rawUrl.Contains("."))
            //{
            //    new IPBO().Save();
            //    new PVBO().Save();
            //}


            //if (requestUrl.Contains("index.aspx") || (requestUrl.ToLower().Contains("/website") && requestUrl.Contains(".aspx")))
            //{
            //    (new IPBO()).Save();
            //    (new PVBO()).Save();
            //}

            ////mvc用
            //if (requestUrl == "/" || requestUrl.ToLower().Contains("/site/"))
            //{
            //    if (!requestUrl.Contains("."))
            //    {
            //        (new IPBO()).Save();
            //        (new PVBO()).Save();
            //    }
            //}
        }

    }
}